<template>
  <div class="app-container">
    <!-- 条件过滤区 -->
    <div class="filter-container">
      <div class="filter-row">
        <div class="filter-item">
          <span class="filter-title">站点名称：</span>
          <el-input placeholder="请输入站点名称" size="small"
                class="search-item" v-model="searchObj.station"/>
        </div>
        <div class="filter-item">
          <span class="filter-title">风险类型：</span>
          <el-radio-group v-model="searchObj.riskType" size="small">
            <el-tooltip content="包括以下行为之一则视为风险行为：(1)路过港口城市 (2)中高风险区域 (3)从阳性病例城市经过" placement="top">
              <el-radio-button :label="1">风险行为</el-radio-button>
            </el-tooltip>
            <el-radio-button :label="2">申报风险</el-radio-button>
          </el-radio-group>
          <!-- <span class="filter-title">是否高风险：</span>
          <el-select size="small" v-model="searchObj.isRisk" class="short-search-item" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select> -->
        </div>
        <!-- <div class="filter-item">
          <span class="filter-title">是否申报进入：</span>
          <el-select size="small" v-model="searchObj.isReportRiskEntry" class="short-search-item" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </div> -->
        <!-- <div class="filter-item">
          <span class="filter-title">是否申报离开：</span>
          <el-select size="small" v-model="searchObj.isReportRiskExit" class="short-search-item" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </div> -->
      </div>
      <div class="filter-row">
        
        <div class="filter-item">
          <span class="filter-title">所属区域：</span>
            <el-cascader
            size="small"
            class="search-item"
            :props="props"
            @change="handleChange"
            v-model="searchObj.areaIds"
          ></el-cascader>
        </div>
        <div class="filter-item">
          <span class="filter-title">统计日期：</span>
           <el-date-picker
            size="small"
            class="search-item date-range-item"
            v-model="searchObj.timeRange"
            type="datetimerange"
            format="yyyy-MM-dd  HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            :range-separator="null"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </div>
        
        <div class="filter-item">
          <el-button size="small" @click="search" class="" type="primary">
          查询
          </el-button>
          <el-button size="small" @click="reset" class="">
            重置
          </el-button>
          <el-button type="primary" :loading="exportLoading" size="small" @click="exportExcel" class="filter-item" plain>
            导出
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      size="medium"
      :data="tabItems"
      ref="table"
      style="width: 100%;"
      v-loading="tabLoading"
    >
      <el-table-column label="序号" align="center" width="50px"
        type="index" />
      <el-table-column label="站点名称" align="left"
        prop="stName" />
      <!-- <el-table-column label="战点hex" align="center"
        prop="stHex"
      /> -->
      <el-table-column label="风险数" align="center"
        prop="riskCount"
      />
      <!-- <el-table-column label="入站数" align="center"
        prop="exitCount"
      /> -->
      <el-table-column label="出站数" align="center"
        prop="exitCount"
      />
      <el-table-column label="操作" align="center"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            style="margin-right: 0px"
            @click="itemDetail(scope.row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-box">
      <el-pagination
        :background="true"
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        :page-count="pagination.pageCount"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import areaConvert from '@/utils/areaCodeCov.js';
import { selectStationRisk, exportStationRisk, commAreaUrl, commUserAreaUrl  } from '@/http/apiUrls.js';
import { getDateBefore } from '@/utils/date.js';
import { yesOrNo } from '@/data/vehiclePro.js';
export default {
  name: 'VehicleReportList',
  data() {
    return {
      tabLoading: false,
      exportLoading: false,
      tableKey: 0,
      pageItems: 10, // 每页查询条数
      pagination: {
        currentPage: 1, // 当前页
        pageSize: 10, // 每页显示条数
        pageCount: 1, // 总页数
      },
      tabItems: [
      ],
      props: {
        lazy: true,
        checkStrictly: true,
        lazyLoad: (node, resolve) => {
          const { level } = node;
          const nodes = [];
          let type = level == 0 ? null : node.value;
          this.provinceFn(type)
            .then((res) => {
              // 节点数组
              res.map((item) => {
                let obj = {
                  value: item.code,
                  label: item.fullName,
                  leaf: item.level >= 4
                  // leaf: node.level >= 3, 
                };
                nodes.push(obj);
              });
              // resolve 节点返回
              resolve(nodes);
            })
            .catch((error) => {
              console.log(error);
            });
        }
      },
      searchObj: this.initSearchObj(),
      options: yesOrNo
    }
  },
  created(){
    const {isRisk, areaIds, startTime, endTime, isReportRiskEntry, isReportRiskExit} = this.$route.query

    if(isRisk === '1'){
      this.searchObj.isRisk = true
      if(startTime && endTime){
        this.searchObj.timeRange = [startTime, endTime]
      }
      if(areaIds){
        this.searchObj.areaIds = areaIds.split(',')
      }
      if(isReportRiskEntry){
        this.searchObj.isReportRiskEntry = (isReportRiskEntry === '1')?true:this.searchObj.isReportRiskEntry
      }
      if(isReportRiskExit){
        this.searchObj.isReportRiskExit = (isReportRiskExit === '1')?true:this.searchObj.isReportRiskExit
      }
    }
    this.getData(1, this.pageItems, this.getSearchObj(), {})
  },
  methods: {
    initSearchObj(){
      let date = getDateBefore(1)
      return {
        station: '',
        timeRange: [
          date + ' 00:00:00',
          date + ' 23:59:59'
        ],
        areaIds: [],
        isRisk: '',
        isReportRiskEntry: '',
        riskType: 1,
      }
    },
    getSearchObj(){
      let obj = {...this.searchObj};
      if(obj.timeRange && obj.timeRange.length == 2 ){
        obj.startTime = obj.timeRange[0]
        obj.endTime = obj.timeRange[1]
      }
      const ids = obj.areaIds;
      let areaCode = '';
      if(ids && ids.length > 0){
        areaCode = ids[ids.length-1]
        let likeVal = areaConvert(areaCode);
        obj.areaCode = likeVal
      }
      if(obj.riskType == 1){
        obj.isRisk = true;
        obj.isReportRiskEntry = null;
      }else if(obj.riskType == 2){
        obj.isRisk = null;
        obj.isReportRiskEntry = true;
      }
      return obj;
    },
    getExtraObj(){
      let extra = {};
      return extra
    },
    search(){
      this.getData(1, this.pageItems, this.getSearchObj(), this.getExtraObj())
    },
    reset(){
      this.searchObj = this.initSearchObj()
       this.getData(1, this.pageItems, this.getSearchObj(), {})
    },
    getData(current , size, model, extra){
      let requestData = {
        current,
        size,
        model,
        extra
      }
      this.tabLoading = true;
      this.$postJson(selectStationRisk, requestData).then(res => {
        this.tabLoading = false;
        if(res){
          const pagination = {
            currentPage: parseInt(res.current), // 当前页面号
            pageSize: parseInt(res.size), // 每页条目数
            pageCount: parseInt(res.pages), // 总页面数
          }
          this.pagination = pagination;
          this.tabItems = res.records;
        }
      })
    },
    getArea(code){
      return new Promise((resolve, reject) => {
        const requestData = {
          parentCode: code
        }
        if(code){
          this.$postJson(commAreaUrl, requestData).then(res => {
            if(res){
              resolve(res)
            }else{
              resolve([])
            }
          })
        }else{
          this.$postJson(commUserAreaUrl, {}).then(res => {
            if(res){
              resolve(res)
            }else{
              resolve([])
            }
          })
        }
      })
    },
    // 获取省市区街道
    provinceFn(code) {
      // 此处是一个promise 直接返回
      return this.getArea(code);
    },
     // 监听数据变化
    handleChange(value) {
      console.log(value);
    },
    async handleSizeChange(val) {
      this.pageItems = val
      await this.getData(1, this.pageItems, this.getSearchObj(), this.getExtraObj())
    },
    async handleCurrentChange(val) {
      await this.getData(val, this.pageItems, this.getSearchObj(), this.getExtraObj())
    },
    itemDetail(item){
      let {areaIds, startTime, endTime, riskType} = this.getSearchObj()
      this.$router.push({
        path: "/fk/vehicleReportDetail",
        query: {
          station: item.stName,
          areaIds:areaIds.join(','), 
          startTime, 
          endTime,
          riskType
        }
      })
    },
    exportExcel() {
      let requestData = {
        model: this.getSearchObj()
      }
      this.exportLoading = true
      this.$exportJson(exportStationRisk, requestData).then(res => {
        this.exportLoading = false
        if(res){
          var b = new Blob([res], { type: "application/vnd.ms-excel" });
          // 根据传入的参数b创建一个指向该参数对象的URL
          var url = URL.createObjectURL(b);
          var link = document.createElement("a");
          // 导出的文件名
        
          let fileName = "站点排查统计.xlsx";
          link.download = fileName;
          link.href = url;
          link.click();
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  .filter-row {
    width: 100%;
    margin-bottom: 8px;
    display: flex;
    .filter-item {
      width: fit-content;
      .filter-title {
        display:inline-block;
        width:100px;
        text-align:right;
        margin-left: 4px;
      }
    }
  }
  .tabs-filter-container{
    margin-bottom: 10px;
  }
}
</style>